(function($, doc) {
	$.plusReady(function() {
		$.init();
		var user = app.getState();
		/*
		doc.getElementById('ethck').addEventListener('click', function() {
			doc.getElementById('addrshow').style.display = 'none';
		});
		doc.getElementById('btcck').addEventListener('click', function() {
			doc.getElementById('addrshow').style.display = 'block';
		});*/
		doc.getElementById("confimbtn").addEventListener('tap', function() {

			var ctype = document.querySelector('[name="radio1"]:checked').value;
			//console.log(ctype);
			var my = 1;
			[].forEach.call(doc.getElementsByClassName("mui-active"), function(item, index) {
				if(index) {
					my = item.id.replace("item", "") * 1;
				}
			});
			var val = doc.getElementById('textarea' + my).value;

			if(!val || val == '') {
				var msg = '助记词';
				if(my == 2) {
					msg = '密钥';
				}
				/*
				if(my == 3) {
					msg = 'keystore';
				}*/
				$.toast(msg + '内容不能为空');
				return false;
			}
			//console.log(val);
			var passwd = doc.getElementById('passwd').value;
			var repasswd = doc.getElementById('repasswd').value;
			var walletname = doc.getElementById('walletname').value;
			if(!walletname) {
				$.toast('钱包名称不能为空');
				return false;
			}
			if(app.checkPwd(passwd, 6) === false) {
				$.toast('钱包密码必须字母加数字6位以上');
				return;
			}
			if(passwd !== repasswd) {
				$.toast('两次输入密码不一致！');
				return;
			}
			//console.log(val);
			//console.log(my);

			if(my == 1) {
				if(ctype == 'eth') {
					var wallet = new ethers.Wallet.fromMnemonic(val);
					//console.log(JSON.stringify(wallet));
					//console.log("Address: " + wallet.address);
					if(!wallet || !wallet.address) {
						$.toast("助记词格式错误");
						return;
					}
					var adddata = {
						name: walletname + '(eth)',
						address: wallet.address,
						passwd: md5(passwd),
						privkey: wallet.privateKey,
						mnemonic: wallet.mnemonic,
						path: wallet.path
					};
					ethscan.addWallet(adddata, 'eth');
					$.toast("钱包导入成功!");
					plus.webview.currentWebview().close();

				} else if(ctype == 'btc') {
					plus.nativeUI.showWaiting("钱包导入中...");
					bitscan.createWallet(val, md5(passwd), function(rt) {
						plus.nativeUI.closeWaiting();
						var adddata = {
							'name': walletname + '(btc)',
							'privkey': rt.sec,
							'address': rt.address,
							'mnemonic': val,
							'passwd': md5(passwd)
						};
						ethscan.addWallet(adddata, 'btc');
						$.toast("钱包导入成功!");
						plus.webview.currentWebview().close();

					});
				} else if(ctype == 'eos') {
					var ecc = eosjs_ecc;
					var privkey = ecc.seedPrivate(val);
					var addr = ecc.privateToPublic(privkey);
					//console.log(privkey);
					//console.log(addr);
					var adddata = {
						'name': walletname + '(eos)',
						'privkey': privkey,
						'address': addr,
						'mnemonic': val,
						'passwd': md5(passwd)
					};
					ethscan.addWallet(adddata, 'eos');
					$.toast("钱包导入成功!");
					plus.webview.currentWebview().close();
				}

			}
			if(my == 2) {
				if(ctype == 'eth') {
					console.log(val.length);
					if(val.length == 64) {
						val = '0x' + val;
					}
					if(val.length !== 66) {
						$.toast('密钥长度错误！');
						return;
					}
					var wallet = new ethers.Wallet(val);
					if(!wallet || !wallet.address) {
						$.toast("密钥格式错误");
						return;
					}
					var adddata = {
						name: walletname + '(eth)',
						address: wallet.address,
						passwd: md5(passwd),
						privkey: wallet.privateKey,
						mnemonic: wallet.mnemonic,
						path: wallet.path
					};
					ethscan.addWallet(adddata, 'eth');
					$.toast("钱包导入成功!");
					plus.webview.currentWebview().close();
					
				} else if(ctype == 'btc'){
					//var addr = doc.getElementById('address').value;
					var eckey = new Bitcoin.ECKey(val);
					addr = eckey.getBitcoinAddress().toString();
					var adddata = {
						'name': walletname + '(btc)',
						'privkey': val,
						'address': addr,
						'mnemonic': '',
						'passwd': md5(passwd)
					};
					ethscan.addWallet(adddata, 'btc');
					$.toast("钱包导入成功!");
					plus.webview.currentWebview().close();
					
				} else if(ctype == 'eos') {
					
					var ecc = eosjs_ecc;
					var privkey = val;
					var addr = ecc.privateToPublic(privkey);
					//console.log(privkey);
					//console.log(addr);
					var adddata = {
						'name': walletname + '(eos)',
						'privkey': privkey,
						'address': addr,
						'mnemonic': '',
						'passwd': md5(passwd)
					};
					ethscan.addWallet(adddata, 'eos');
					$.toast("钱包导入成功!");
					plus.webview.currentWebview().close();
				
				}

			}
			
		});
	});
}(mui, document));